package com.xiaoma.verbal.scheduler;

import static com.xiaoma.verbal.utils.Constants.FTC;
import static com.xiaoma.verbal.utils.Constants.QCTC;
import static com.xiaoma.verbal.utils.Constants.QTTC;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.xiaoma.tpo.domain.User;
import com.xiaoma.tpo.service.UserService;
import com.xiaoma.verbal.component.AsynPushClient;
import com.xiaoma.verbal.domain.ServiceApi;
import com.xiaoma.verbal.dto.CorrectedAudioVo;
import com.xiaoma.verbal.service.CacheService;
import com.xiaoma.verbal.service.ForecastService;
import com.xiaoma.verbal.service.PrctPracticeAudioService;
import com.xiaoma.verbal.service.QuClassicService;
import com.xiaoma.verbal.service.QuTpoService;
import com.xiaoma.verbal.service.QuestionService;
import com.xiaoma.verbal.service.ServiceApiService;
import com.xiaoma.verbal.utils.Constants;
import com.xiaoma.verbal.utils.MemoryDB;
import com.xiaoma.verbal.utils.Pagination;
import com.xiaoma.verbal.webservice.VerbalQuestionWS;

@Component("prctAssistantTask")
public class PrctAssistantTask {
	private static Logger LOG = LoggerFactory.getLogger(PrctAssistantTask.class);
	@Autowired
	private PrctPracticeAudioService prctPracticeAudioService;
	@Autowired
	private UserService userService;
	@Autowired
	private ServiceApiService serviceApiService;
	@Autowired
	private AsynPushClient asynPushClient;

	// @Scheduled(cron = "0 0 2 * * *")
	@Scheduled(fixedRate = 60*60*1000)
	public void reload() {
		try {
			List<Object[]> messages = new ArrayList<Object[]>();
			Pagination page = prctPracticeAudioService.list(null, null, null, 1, 1, 100);
			List<CorrectedAudioVo> list = (List<CorrectedAudioVo>) page.getList();
			String postUrl = serviceApiService.findByUniqueCode(Constants.API_XBPG).getApi();
			for (CorrectedAudioVo audio : list) {
				Object[] parms = new Object[3];
				parms[0] = postUrl;
				parms[1] = this.prctPracticeAudioService.toJsonString(audio);
				parms[2] = audio.getId();
				messages.add(parms);
			}
			if (messages.size() > 0) {
				asynPushClient.init(messages);
				try {
					asynPushClient.asynPost();
				} catch (Exception e) {
					e.printStackTrace();
					// TODO: handle exception
				}

			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
